package com.example.sqliteapplication.utils;

import android.app.Application;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.sqliteapplication.MyDBHelper;
import com.example.sqliteapplication.bean.Student;

import java.util.ArrayList;

/**
 * Created by wly on 2019/4/18.
 */

public class DBUtils {
    private static  SQLiteDatabase db;
    private static MyDBHelper helper;
    static{
        helper=new MyDBHelper(MyAppInfo.getAppContext());
        db=helper.getReadableDatabase();
    }



    public static void insert(Context context ,Object[]objs){

        db.execSQL("insert into info(_id,name,phone) values(?,?,?)",objs);

    }
    public static void deleteByName(Context context,String name){
        helper=new MyDBHelper(context);
        db=helper.getReadableDatabase();
        db.execSQL("delete from info where name=?",new String[]{name});
    }
    public static void updateName(Context context,String old,String newname){
        helper=new MyDBHelper(context);
        db=helper.getReadableDatabase();
        db.execSQL("update info set name=? where name=?",new String[]{newname,old});
    }
    public static ArrayList<Student> findall(Context context){
        helper=new MyDBHelper(context);
        db=helper.getReadableDatabase();
        Cursor cursor=db.rawQuery("select * from info",null);
        ArrayList<Student> students=new ArrayList<>();
        while(cursor.moveToNext()){
            Student s =new Student(cursor.getString(0),cursor.getString(1),cursor.getString(2));
            students.add(s);
        }
        return students;
    }
}
